package com.seocoo.worker.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import com.seocoo.worker.R;
import com.seocoo.worker.base.Const;
import com.seocoo.worker.base.PermissionUtil;
import com.seocoo.worker.base.entity.ELogin;
import com.seocoo.worker.base.entity.EVersion;
import com.seocoo.worker.base.menu.NavigationButton;
import com.seocoo.worker.base.menu.OnTabReselectListener;
import com.seocoo.worker.user.model.UserModel;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.event.BusEvent;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import cn.jpush.android.api.JPushInterface;

public class HomeActivity extends MVVMActivity implements NavFragment.OnNavigationReselectListener {
    public static void startContentUI(Context context) {

        Intent intent = new Intent(context, HomeActivity.class);
        context.startActivity(intent);
    }

    private NavFragment mNavBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        this.init();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (TextUtils.equals("open.order", getIntent().getStringExtra("action"))) {
            int index = getIntent().getIntExtra("index", 0);
            mNavBar.onSelectTab(index);
        }
    }

    private void init() {
        FragmentManager manager = getSupportFragmentManager();
        mNavBar = ((NavFragment) manager.findFragmentById(R.id.homeFagNav));
        mNavBar.setup(this, manager, R.id.homeContainer, this);

        if (!PermissionUtil.isOpenGPS(this)) {
            // 未打开GPS or 没有GPS 定位权限
            LocationDialog.showDialogUI(this);
        } else {

        }

        //更新工匠的位置信息
        getViewModel(UserModel.class).refreshLocation(getApplication());


        ELogin login = AppCacheManager.getCacheListener().getObject(Const.LOGIN_KEY, ELogin.class);
        if (login != null) {
            JPushInterface.setAlias(getApplicationContext(), 10001, "dwm_"+login.getUserInfo().getUserId());
        }
        //版本更新检查
        getViewModel(UserModel.class).queryVersion();
    }

    @Override
    public void onReselect(NavigationButton navigationButton) {
        Fragment fragment = navigationButton.getFragment();
        if (fragment != null
                && fragment instanceof OnTabReselectListener) {
            OnTabReselectListener listener = (OnTabReselectListener) fragment;
            listener.onTabReselect();
        }
    }

    @LiveDataMatch
    public void onVersonDialog(EVersion version) {
        if (version != null && version.show) {
            VersionDialog.showDialogUI(this, version);
        }
    }

    @BusEvent(from = "个人中心切换")
    public void onChangeTab(BusEventTab tab) {
        mNavBar.onSelectTab(tab.index);
    }
}
